package com.mystore.manage.controller;

import com.mystore.common.paging.StylePage;
import com.mystore.framework.pojo.App;
import com.mystore.framework.service.impl.IAppModelServiceImpl;
import com.mystore.framework.service.impl.IAppServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

/**
 * 应用表(App)表控制层
 */
@Controller
@RequestMapping("manage/app")
public class AppController {
    /**
     * 服务对象
     */
    @Resource
    private IAppServiceImpl iAppServiceImpl;
    @Resource
    private IAppModelServiceImpl iAppModelServiceImpl;
    @GetMapping("/page.do")
    public String page(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<App> appPage = iAppServiceImpl.selectPageByData(data,new Page<>(current, size));
        model.addAttribute("apps", new StylePage<>(appPage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "app_page";
    }

    @GetMapping("/insert.do")
    public String insert(Model model) {
        model.addAttribute("mode", "insert");
        return "app_info";
    }

    @GetMapping("/update.do")
    public String update(Model model, int appId) {
        model.addAttribute("app", iAppServiceImpl.selectById(appId));
        model.addAttribute("mode", "update");
        return "app_info";
    }

    @PostMapping("/save.do")
    public String save(RedirectAttributes model, App app, String mode) {
        if(StringUtils.hasText(app.getAppImage())){
            if(app.getAppImage().endsWith(","))
                app.setAppImage(app.getAppImage().substring(0,app.getAppImage().length()-1));
        }

        if (mode.equals("insert"))
            model.addAttribute("result", iAppServiceImpl.insertOne(app)>0);
        else
            model.addAttribute("result", iAppServiceImpl.updateOne(app));
        model.addAttribute("mode", mode);
        return "redirect:/manage/app/page.do";
    }

    @GetMapping("/delete.do")
    public String delete(RedirectAttributes model, int appId) {
        model.addAttribute("result", iAppServiceImpl.deleteById(appId));
        return "redirect:/manage/app/page.do";
    }
    @GetMapping("/onlinePage.do")
    public String online(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<App> appPage = iAppServiceImpl.selectOfflinePageByData(data,new Page<>(current, size));
        model.addAttribute("apps", new StylePage<>(appPage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "app_online";
    }
    @GetMapping("/offlinePage.do")
    public String offline(@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<App> appPage = iAppServiceImpl.selectOnlinePageByData(data,new Page<>(current, size));
        model.addAttribute("apps", new StylePage<>(appPage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        return "app_offline";
    }
    @GetMapping("/onlineApp.do")
    public String onlineApp(RedirectAttributes model, int appId){
        model.addAttribute("result", iAppServiceImpl.onlineApp(appId));
        return "redirect:/manage/app/offlinePage.do";
    }
    @GetMapping("/offlineApp.do")
    public String offlineApp(RedirectAttributes model, int appId){
        model.addAttribute("result", iAppServiceImpl.offlineApp(appId));
        return "redirect:/manage/app/offlinePage.do";
    }
    @GetMapping("/updateAppVersion.do")
    public String updateAppVersion(String msg,@RequestParam(defaultValue="") String data,Model model, @RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size,String result) {
        IPage<App> appPage = iAppServiceImpl.selectOnlinePageByData(data,new Page<>(current, size));
        model.addAttribute("apps", new StylePage<>(appPage));
        model.addAttribute("result",result);
        model.addAttribute("data",data);
        model.addAttribute("msg",msg);
        return "update_app_version";
    }
    @GetMapping("/updateAppPage.do")
    public String updateAppPage(RedirectAttributes attributes,Model model,int appId) {
        if(iAppServiceImpl.selectById(appId)!=null){
            model.addAttribute("appId",appId);
            return "update_app_info";
        }else{
            attributes.addAttribute("result",false);
            return "redirect:/manage/app/updateAppVersion.do";
        }
    }
    @PostMapping("/saveUpdateApp.do")
    public String saveUpdateApp(RedirectAttributes attributes,Model model,int appId,String appVersion,String[] platforms,int[] fileModelIds) {
        if(iAppServiceImpl.selectById(appId)!=null){
            if(!iAppModelServiceImpl.deleteByAppId(appId)) {
                attributes.addAttribute("result",false);
                return "redirect:/manage/app/updateAppVersion.do";
            }
            if(iAppModelServiceImpl.insertApps(appId,appVersion,platforms,fileModelIds)){
                attributes.addAttribute("result",true);
            }else{
                attributes.addAttribute("result",false);
            }
        }else{
            attributes.addAttribute("result",false);
        }
        return "redirect:/manage/app/updateAppVersion.do";
    }
}

